<?php
/**
 * Groups module for XOOPS
 *
 * You may not change or alter any portion of this comment or credits
 * of supporting developers from this source code or any supporting source code 
 * which is considered copyrighted (c) material of the original comment or credit authors.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 *
 * @copyright       The Groups Project http://code.google.com/p/groupsforxoops/
 * @license         http://www.fsf.org/copyleft/gpl.html GNU public license
 * @package         groups
 * @since           2.3.0
 * @author          Hui Xiao < xh.8326@gmail.com >
 * @version         $Id: topics.php Sat Jul 18 13:20:12 CST 2009
 */

include "header.php";

$group_id = isset($_GET["group_id"]) ? intval($_GET["group_id"]) : 0;
$key = isset($_GET["key"]) ? trim($_GET["key"]) : "";

$group_handler = xoops_getmodulehandler("groups","groups");
$threads_handler = xoops_getmodulehandler("threads","groups");
$category_handler = xoops_getmodulehandler("category","groups");

include_once(dirname(__FILE__)."../../include/form.cp.topic.search.php");

$start = isset($_GET["start"]) ? intval($_GET["start"]) : 0 ;
$perPage = 20;
$extar = "";
$criteria = new CriteriaCompo();
if ( !empty($group_id) ) {
    $_group_id = ( $group_id < 0 ) ? 0 : $group_id;
    $criteria->add(new Criteria("group_id",$_group_id));
    $extar .= "group_id=".$_group_id;
}
if( !empty($key) ) {
    $criteria->add(new Criteria("threads_subject","%".$key."%","LIKE"));
    $extar .= "key=".$key;
}
$criteria->setStart($start);
$criteria->setLimit($perPage);
$criteria->setOrder("DESC");
$criteria->setSort("threads_lastdate");
$pagecount = $threads_handler->getCount($criteria);
if( $pagecount > $perPage){	
	$nav = new XoopsPageNav($pagecount, $perPage, $start, "start",$extar);
	$xoopsTpl->assign("pagenav", $nav->renderNav(4));
}
$threads_objs = $threads_handler->getAll($criteria);
$threads_list = array();
if ( $threads_objs ) {
	foreach ( $threads_objs as $k=>$_obj ) {
		$threads_list[$k] = $_obj->getValues();
		$threads_list[$k]["threads_lastdate"] = $_obj->lastDate();
		$threads_list[$k]["threads_dateline"] = $_obj->dateLineAll();
		$threads_list[$k]["threads_icon"] = $_obj->icon();
	}
	$xoopsTpl->assign(array("threads_list"=>$threads_list));
}
unset($criteria);

$criteria = new CriteriaCompo();
$groups_list = $group_handler->getAll($criteria,array("group_name","cat_id"),false);

$_cats = $category_handler->getAll(null,null,false);

$_groups_tree = array();
foreach ( $_cats as $k=>$val ) {
    $_groups_tree[$k]["name"] = $val["cat_name"];
    foreach ( $groups_list as $i=>$v ) {
        if ( $k == $v["cat_id"] ) {
            $_groups_tree[$k]["tree"][$i] = $v["group_name"];
        }
    }
}
$xoopsTpl->assign("groups_tree",$_groups_tree);
$xoopsTpl->display("db:groups_cp_topics.html");
include "footer.php";

?>